/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.wechatmsg.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.modules.sys.entity.User;
import com.lczyfz.campusinfo.modules.user.service.CampusinfoUserService;
import com.lczyfz.campusinfo.modules.wechatmsg.entity.WeixinGroup;
import com.lczyfz.campusinfo.modules.wechatmsg.service.WeixinGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.wechatmsg.entity.WeixinUserGroup;
import com.lczyfz.campusinfo.modules.wechatmsg.service.WeixinUserGroupService;

import java.util.List;

/**
 * 微信用户组别Controller
 * @author 陈维乾
 * @version 2018-05-06
 */
@Controller
@RequestMapping(value = "${adminPath}/wechatmsg/weixinUserGroup")
public class WeixinUserGroupController extends BaseController {

	@Autowired
	private WeixinUserGroupService weixinUserGroupService;

	@Autowired
	private CampusinfoUserService campusinfoUserService;

	@Autowired
	private WeixinGroupService weixinGroupService;

	@ModelAttribute
	public WeixinUserGroup get(@RequestParam(required=false) String id) {
		WeixinUserGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = weixinUserGroupService.get(id);
		}
		if (entity == null){
			entity = new WeixinUserGroup();
		}
		return entity;
	}
	
	@RequiresPermissions("wechatmsg:weixinUserGroup:view")
	@RequestMapping(value = {"list", ""})
	public String list(WeixinUserGroup weixinUserGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<WeixinGroup> weixinGroups= weixinGroupService.getAllList();
		model.addAttribute("weixinGroups", weixinGroups);
		Page<WeixinUserGroup> page = weixinUserGroupService.findPage(new Page<WeixinUserGroup>(request, response), weixinUserGroup); 
		model.addAttribute("page", page);
		return "modules/wechatmsg/weixinUserGroupList";
	}

	@RequiresPermissions("wechatmsg:weixinUserGroup:view")
	@RequestMapping(value = "form")
	public String form(WeixinUserGroup weixinUserGroup, Model model) {
			List<User> userList = campusinfoUserService.getWeiXinUser();
			List<WeixinGroup> weixinGroups= weixinGroupService.getAllList();

			model.addAttribute("weixinGroups", weixinGroups);
			model.addAttribute("userList", userList);
			model.addAttribute("weixinUserGroup", weixinUserGroup);
			return "modules/wechatmsg/weixinUserGroupForm";
	}

	@RequiresPermissions("wechatmsg:weixinUserGroup:edit")
	@RequestMapping(value = "save")
	public String save(WeixinUserGroup weixinUserGroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, weixinUserGroup)){
			return form(weixinUserGroup, model);
		}
		int resultCount = weixinUserGroupService.isUniqueness(weixinUserGroup);
		if(resultCount > 0){
			addMessage(redirectAttributes, "保存微信用户组别信息失败，请勿重复添加");
		}else{
			weixinUserGroupService.save(weixinUserGroup);
			addMessage(redirectAttributes, "保存微信用户组别成功");
		}
		return "redirect:"+Global.getAdminPath()+"/wechatmsg/weixinUserGroup/?repage";
	}
	
	@RequiresPermissions("wechatmsg:weixinUserGroup:edit")
	@RequestMapping(value = "delete")
	public String delete(WeixinUserGroup weixinUserGroup, RedirectAttributes redirectAttributes) {
		weixinUserGroupService.delete(weixinUserGroup);
		addMessage(redirectAttributes, "删除微信用户组别成功");
		return "redirect:"+Global.getAdminPath()+"/wechatmsg/weixinUserGroup/?repage";
	}

}